//
//  ProductCategoriesVC.m
//  AFNetworking
//
//  Created by admin on 2020/4/28.
//

#import "ProductCategoriesVC.h"
#import "CategoriesTableView.h"
#import "PageHeaderCollectionView.h"
#import "ProudctCollectionCell.h"
#import "UGAlignmentButton.h"


@interface ProductCategoriesVC ()

@property(strong, nonatomic) PageHeaderCollectionView *headView; //顶部

@property(strong, nonatomic)CategoriesTableView  *categoriesTableView;//左侧类型

@property(strong, nonatomic) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray * datalist;

@property (nonatomic, strong) UGAlignmentButton * manageCategoriesBtn;//分类管理
@property (nonatomic, strong) UIButton * createProsBtn;//添加商品

@end

@implementation ProductCategoriesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

-(void)configUI{
    self.title = @"商品管理";
     [self.view addSubview:self.headView];
    [self.view addSubview:self.categoriesTableView];
    [self.view addSubview:self.tableView];
      [self.view addSubview:self.createProsBtn];
      [self.view addSubview:self.manageCategoriesBtn];
}
-(NSMutableArray *)datalist{
    if (!_datalist) {
        _datalist = [@[
            @{
                @"title":@"热销",
                @"data":@[@"0",@"1"]
            },
            @{
                @"title":@"热卖",
                @"data":@[@"0",@"1"]
            },
            @{
                @"title":@"新品上市",
                @"data":@[@"0",@"1"]
            },
            @{
                @"title":@"宝藏鲜奶 茶",
                @"data":@[@"04-01",@"04-02",@"04-03"]
            },
            @{
                @"title":@"吐司",
                @"data":@[@"04-01",@"04-02",@"04-03"]
            },
            @{
                @"title":@"霸气芝士 鲜果茶",
                @"data":@[@"04-01",@"04-02",@"04-03"]
            },
        ] mutableCopy];
    }
    return _datalist;
}
static NSInteger handlecount = 0;
-(CategoriesTableView *)categoriesTableView{
    if (!_categoriesTableView) {
        _categoriesTableView = [CategoriesTableView new];
        [_categoriesTableView setDatalist:self.datalist];
        UG_WEAKSELF
        [_categoriesTableView setCellClickBlock:^(NSIndexPath * _Nonnull indexPath) {
            NSIndexPath *index = [NSIndexPath indexPathForItem:0 inSection:indexPath.row];
            handlecount++;
            [weakSelf.tableView scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionNone animated:YES];
            [weakSelf bk_performBlock:^(id obj) {
                handlecount--;
            } afterDelay:0.5];
        }];
    }
    return _categoriesTableView;
}
-(PageHeaderCollectionView *)headView{
    if (!_headView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(KWidth/4, 44);
        _headView = [[PageHeaderCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        NSArray *items = [NSArray modelArrayWithClass:[PageHeaderCollectionCellData class] json:@[
            @{@"title":@"全部" },
            @{@"title":@"已下架" },
            @{@"title":@"在售中" },
            @{@"title":@"待平台审核" }
        ]];
        [_headView setItems:items];
        UG_WEAKSELF
        [_headView setCellClick:^(id  _Nonnull item) {
//            [weakSelf reloadData];
        }];
        _headView.backgroundColor = UIColor.whiteColor;
    }
    return _headView;
}
-(UIButton *)createProsBtn{
    if (!_createProsBtn) {
        _createProsBtn = [UIButton new];
        [_createProsBtn setTitle:@"新增\n商品" forState:UIControlStateNormal];
         [_createProsBtn setBackgroundColor:[UIColor ug_hexString:@"#007AFF"]];
        [_createProsBtn ug_radius:KAutoAcale(65/2)];
        _createProsBtn.titleLabel.numberOfLines = 2;
        _createProsBtn.titleLabel.font = FONT_SYS16;
        [_createProsBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
            [self routepush:@"ProductEditVC" param:@{}];
        }];
    }
    return _createProsBtn;
}
-(UGAlignmentButton *)manageCategoriesBtn{
    if (!_manageCategoriesBtn) {
        _manageCategoriesBtn = [UGAlignmentButton new];
        _manageCategoriesBtn.titleLabel.font  = FONT_SYS15;
        _manageCategoriesBtn.backgroundColor = UIColor.whiteColor;
        [_manageCategoriesBtn setTitleColor:[UIColor ug_hexString:@"#007AFF"] forState:UIControlStateNormal];
        _manageCategoriesBtn.ug_Alignment = UGAlignmentLeft;
        [_manageCategoriesBtn setImage:[UIImage imageWithIcon:@"fa-angle-right" backgroundColor:[UIColor clearColor] iconColor:[UIColor ug_hexString:@"#007AFF"] andSize:CGSizeMake(15, 15)] forState:UIControlStateNormal];
        [_manageCategoriesBtn setTitle:@"分类管理" forState:UIControlStateNormal];
        [_manageCategoriesBtn ug_addEvents:UIControlEventTouchUpInside andBlock:^(id  _Nonnull sender) {
            [self routepush:@"ProductCategoriesManage" param:@{}];
        }];
    }
    return _manageCategoriesBtn;
}


-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.separatorColor = COLOR_EE;

    }
    return _tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datalist.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dic = [_datalist objectAtIndex:section];
    NSArray *arr = [dic objectForKey:@"data"];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indentifier = @"ProudctCollectionCell";
    ProudctCollectionCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[ProudctCollectionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KAutoAcale(102);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ProudctCollectionHeader *headerView = [ProudctCollectionHeader new];
    NSDictionary *dic = [_datalist objectAtIndex:section];

    headerView.titleLab.text = [dic objectForKey:@"title"];
    
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (handlecount!=0) {
        return;
    }
    NSInteger selectection =  GID_MAX;
    NSArray *indexPaths = [_tableView indexPathsForVisibleRows];
    for (NSIndexPath *path in indexPaths) {
        selectection = MIN(selectection, path.section);
    }
    if (selectection == GID_MAX) {
        return;
    }
    NSDictionary *dic = [_datalist objectAtIndex:selectection];
    [_categoriesTableView setSelectData:dic];
}


-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.mas_equalTo(self.view);
           make.right.mas_equalTo(self.view);
           make.top.mas_equalTo(self.view);
           make.height.mas_equalTo(44);
       }];
    
    [_categoriesTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.top.mas_equalTo(self.headView.mas_bottom).mas_offset(8);
        make.width.mas_equalTo(KAutoAcale(120));
        make.bottom.mas_equalTo(self.manageCategoriesBtn.mas_top);
    }];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.categoriesTableView.mas_right);
        make.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.headView.mas_bottom).mas_offset(8);;
        make.bottom.mas_equalTo(self.view);
    }];
    [_createProsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view).mas_offset(KAutoAcale(-15));
        make.bottom.mas_equalTo(self.view).mas_offset(-KAutoAcale(35)-KSAFE_Bottom);
        make.size.mas_equalTo(CGSizeMake(KAutoAcale(65), KAutoAcale(65)));
    }];
    [_manageCategoriesBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(self.categoriesTableView);
        make.bottom.mas_equalTo(self.view).mas_offset(-KSAFE_Bottom);
    }];
}

@end
